package com.example.demo.innoket.factory;

import com.example.demo.innoket.db.DataSource;
import com.example.demo.innoket.db.DbFactory;
import com.example.demo.innoket.generate.Generate;
import com.example.demo.innoket.generate.JavaGenerate;
import com.example.demo.innoket.generate.JsGenerate;
import com.example.demo.innoket.generate.JspGenerate;
import com.example.demo.innoket.util.ConvertHandler;
import com.example.demo.innoket.util.FileType;
import com.example.demo.innoket.util.GeneratorConfig;
import com.example.demo.innoket.vo.Table;

/**
 * 代码生成器工厂
 * @author zhangLi
 *
 */
public class CodeGenerateFactory {

	private static Generate gen;
	private static DataSource db = null;
	private static Table table = null;
	protected static GeneratorConfig.ModulePath path = null;

	/**
	 *  初始化
	 * @param tableName　表名
	 */
	public static void init(String tableName, GeneratorConfig.ModulePath pathValue) {
		try {
			db = DbFactory.create();
			table = db.getTable(tableName);
			ConvertHandler.tableHandle(table);
			path = pathValue;
			System.out.println("init:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建entity
	 */
	public static void createEntity() {
		try {
			gen = new JavaGenerate(FileType.ENTITY);
			gen.generate(table,path);
			System.out.println("createEntity:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建model
	 */
	public static void createModel() {
		try {
			gen = new JavaGenerate(FileType.MODEL);
			gen.generate(table,path);
			System.out.println("createModel:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建dao
	 */
	public static void createDao() {
		try {
			gen = new JavaGenerate(FileType.DAO);
			gen.generate(table,path);
			System.out.println("createDao:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建service
	 */
	public static void createService() {
		try {
			gen = new JavaGenerate(FileType.SERVICE);
			gen.generate(table,path);
			System.out.println("createService:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建serviceImpl
	 */
	public static void createServiceImpl() {
		try {
			gen = new JavaGenerate(FileType.SERVICE_IMPL);
			gen.generate(table,path);
			System.out.println("createServiceImpl:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建DubboProvider
	 */
	public static void createDubboProvider() {
		try {
			gen = new JavaGenerate(FileType.DUBBOPROVIDER);
			gen.generate(table,path);
			System.out.println("createDubboProvider");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建createController
	 */
	public static void createController() {
		try {
			gen = new JavaGenerate(FileType.CONTROLLER);
			gen.generate(table,path);
			System.out.println("createController:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 创建createConfig
	 */
	public static void createConfig() {
		try {
			gen = new JavaGenerate(FileType.CONFIG);
			gen.generate(table,path);
			System.out.println("createConfig:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/***
	 * 创建createRecevier
	 */
	public static void createRecevier() {
		try {
			gen = new JavaGenerate(FileType.RECEVIER);
			gen.generate(table,path);
			System.out.println("createRecevier:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建createApiController
	 */
	public static void createApiController() {
		try {
			gen = new JavaGenerate(FileType.CONTROLLER_API);
			gen.generate(table,path);
			System.out.println("createApiController");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建createExpand
	 */
	public static void createExpand() {
		try {
			gen = new JavaGenerate(FileType.EXPAND);
			gen.generate(table,path);
			System.out.println("createExpand:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建createMapper
	 */
	public static void createMapper() {
		try {
			gen = new JavaGenerate(FileType.MAPPER);
			gen.generate(table,path);
			System.out.println("createMapper:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建JspFile
	 */
	public static void createJspFile() {
		try {
			gen = new JspGenerate(FileType.JSP_LIST_FILE);
			gen.generate(table,path);
			gen = new JspGenerate(FileType.JSP_VIEW_FILE);
			gen.generate(table,path);
			gen = new JspGenerate(FileType.JSP_ADD_FILE);
			gen.generate(table,path);
			System.out.println("createJspFile");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建JSFile
	 */
	public static void createJSFile() {
		try {
			gen = new JsGenerate(FileType.JS_LIST_FILE);
			gen.generate(table,path);
			gen = new JsGenerate(FileType.JS_VIEW_FILE);
			gen.generate(table,path);
			gen = new JsGenerate(FileType.JS_ADD_FILE);
			gen.generate(table,path);
			System.out.println("createJSFile:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		//初始化数据库表
		CodeGenerateFactory.init("finance_log", GeneratorConfig.ModulePath.FINANCE_COMMON_BASE);
		//初始化实体类
		CodeGenerateFactory.createEntity();
	}

}
